<?php
// +----------------------------------------------------------------------
// | HkCms 网站前台
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\admin\model\cms\Archives;
use app\admin\model\cms\Model;
use app\index\model\cms\Category;
use think\facade\Db;
use think\facade\Validate;
use think\facade\Request;

class Sitemap extends BaseController
{

    public function index()
    {
        //xiaowu
		$lang = get_curlang();
		$this->view->assign('lang', $lang);
		$this->view->assign('substationurl', "");
		//xiaowu	
        //设置时区的方法
        date_default_timezone_set('prc');
        //获取准确的时间
        $data = date('y-m-d h:i:s', time());

        //获取当前域名
        $articleList = Request::instance()->domain();
        $html = '';
        $html .= '<urlset>';
        $html .= '<url>';
        $html .= '<loc>' . $articleList . '</loc>';
        $html .= ' <lastmod>' . $data .'</lastmod>';
        $html .= '  <priority>1</priority>';
        $html .= '</url>';

        // 获取所有栏目
        $cate = \app\index\model\cms\Category::where('model_id','>',0)->where('lang','=',$lang)->append(['fullurl'])->select()->toArray();
        // 多条可以遍历出来
        foreach ($cate as $ks=>$vs) {
            $html .= '<url>';
            $html .= ' <loc>'.$vs['fullurl'].'</loc>';
            //$html .= ' <title>'.$vs['title'].'</title>';
            $html .= ' <lastmod>' . $data . '</lastmod>';
            $html .= '  <priority>8</priority>';
            $html .= '</url>';
        }
        // 获取每个栏目里面的文档
        foreach ($cate as $key=>$value) {
            // 获取栏目下的所有文档
            $all = controller($value, function ($obj, $model, $category) {
                // url=链接地址，fullurl=带域名
                return $obj->where(['category_id'=>$category['id']])->append(['publish_time_text','fullurl'])->select()->toArray();
                // 多条
            }, 'category');
            // 多条可以遍历出来
            foreach ($all as $k=>$v) {
                if($v['model_id']!=2)
                {
                    $html .= '<url>';
                    $html .= ' <loc>'.$v['fullurl'].'</loc>';
                    $html .= ' <lastmod>'. date('y-m-d h:i:s', $v['publish_time']).'</lastmod>';
                    $html .= '  <priority>0.64</priority>';
                    $html .= '</url>';
                }
            }
        }
        $html .= '</urlset>';
        //最后一个参数是去掉tp字典的根节点，只输出自己的内容
        $result = xml($html, 200, [], ['root_node' => 'xml']);
        return ($result);
    }
    
    public function rss()
    {
        //xiaowu
		$lang = get_curlang();
		$this->view->assign('lang', $lang);
		$this->view->assign('substationurl', "");
		//xiaowu		
        //设置时区的方法
        date_default_timezone_set('prc');
        //获取准确的时间
        $data = date('y-m-d h:i:s', time());

        //获取当前域名
        $articleList = Request::instance()->domain();
        $html = '<rss version="2.0">';
        $html .= '<channel>';     
        $html .= ' <title><![CDATA[' . site('home_title') . ']]></title>';
        $html .= '  <category><![CDATA[' . site('keyword') . ']]></category>';
        $html .= '  <description><![CDATA['. site('description') .']]></description>';
        $html .= '  <link><![CDATA[' . $articleList . ']]></link>';
        $html .= '  <language>'. $lang .'</language>';
        $html .= '  <pubDate>' . $data . '</pubDate>';
        $html .= '  <lastBuildDate>' . $data . '</lastBuildDate>';
        $html .= '  <generator>YBlog RSS Generator</generator>';
        $html .= '  <ttl>5</ttl>';

        // 获取所有栏目
        $cate = \app\index\model\cms\Category::where('model_id','>',0)->where('lang','=',$lang)->append(['fullurl'])->select()->toArray();
        // 多条可以遍历出来
        foreach ($cate as $ks=>$vs) {
            $html .= '<item>';
            $html .= ' <title><![CDATA['.$vs['title'].']]></title>';
            $html .= ' <description><![CDATA['.$vs['seo_desc'].']]></description>';
            $html .= ' <link><![CDATA['.$vs['fullurl'].']]></link>';
            $html .= ' <pubDate><![CDATA[' . $data . ']]></pubDate>';
            $html .= '  <category><![CDATA['.$vs['title'].']]></category>';
            $html .= '</item>';
        }
        // 获取每个栏目里面的文档
        foreach ($cate as $key=>$value) {
            // 获取栏目下的所有文档
            $all = controller($value, function ($obj, $model, $category) {
                // url=链接地址，fullurl=带域名
                return $obj->where(['category_id'=>$category['id']])->append(['publish_time_text','fullurl'])->select()->toArray();
                // 多条
            }, 'category');
            // 多条可以遍历出来
            foreach ($all as $k=>$v) {
                if($v['model_id']!=2)
                {
                    $html .= '<item>';
                    $html .= ' <title><![CDATA['.$v['title'].']]></title>';
                    $html .= ' <description><![CDATA['.$v['description'].']]></description>';
                    $html .= ' <link><![CDATA['.$v['fullurl'].']]></link>';
                    $html .= ' <pubDate><![CDATA['. date('y-m-d h:i:s', $v['publish_time']).']]></pubDate>';
                    $html .= '  <category><![CDATA['.$value['title'].']]></category>';
                    $html .= '</item>';
                }
            }
        }
        $html .= '</channel>';
        $html .= '</rss>';
        //最后一个参数是去掉tp字典的根节点，只输出自己的内容
        $result = xml($html, 200, [], ['root_node' => 'xml']);
        return ($result);
    }
    
    public function sitemap()
    {
		//xiaowu
		$lang = get_curlang();
		$this->view->assign('lang', $lang);
		$this->view->assign('substationurl', "");
		//xiaowu		
		//$dir = root_path().'template'.DIRECTORY_SEPARATOR.'index/'.site('index_theme').'/';	
		$dir = get_template_path('index');
        $viewfile = 'index/sitemaphtml';
		if (!empty($lang)) {
            $viewfilepath = $dir.$viewfile."_".$lang.".html";
            if (file_exists($viewfilepath)) {
                $viewfile .= "_".$lang;
            }
        }
		//xiaowu	
		return $this->view->fetch($viewfile);
    }
}
